package sword.T11;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-03-04 15:40:19 星期五
 */

class Solution {
    /**
     * 等同于 M154
     */
    /*public int minArray(int[] numbers) {
        int left = 0, right = numbers.length-1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (numbers[left] > numbers[right]) {
                if (numbers[mid] >= numbers[left])
                    left = mid + 1;
                else
                    right = mid;
            } else if (numbers[left] < numbers[right]) {
                break;
            } else {
                if (numbers[mid] > numbers[left])
                    left = mid + 1;
                else if (numbers[mid] < numbers[left]) {
                    left++;
                    right = mid;
                } else {
                    left++;
                }
            }
        }
        return numbers[left];
    }*/

    // 二刷
    public int minArray(int[] numbers) {
        int left = 0, right = numbers.length-1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (numbers[left] < numbers[right]) {
                break;
            } else if (numbers[left] > numbers[right]) {
                if (numbers[mid] >= numbers[left])
                    left = mid + 1;
                else
                    right = mid;
            } else {
                if (numbers[mid] < numbers[left]) {
                    left++;
                    right = mid;
                } else if (numbers[mid] > numbers[left]) {
                    left = mid + 1;
                } else {
                    left++;
                }
            }
        }
        return numbers[left];
    }
}